package com.ebraga.trackerclub.android.handler.core;

import com.ebraga.trackerclub.android.dao.entity.UserAndroid;
import com.ebraga.trackerclub.android.exception.handler.BackofficeHandlerException;
import com.ebraga.trackerclub.android.exception.manager.BackofficeLoginOffLineExpireException;
import com.ebraga.trackerclub.android.exception.manager.BackofficeManagerException;
import com.ebraga.trackerclub.android.handler.api.LoginHandler;
import com.ebraga.trackerclub.android.manager.api.LoginManager;
import com.ebraga.trackerclub.android.vo.UserVO;

public abstract class LoginHandlerImpl implements LoginHandler {
	
	protected LoginManager loginManager;
	
	protected void setLoginManager(LoginManager loginManager) {
		this.loginManager = loginManager;
	}
	
	@Override
	public void loginOnline(UserVO userVO) throws BackofficeHandlerException {
		
		try {
			
			UserAndroid userAndroid = parseToUserAndroid(userVO);
			loginManager.loginOnline(userAndroid);
			
		} catch (BackofficeManagerException e) {
			
			throw new BackofficeHandlerException(e.getMessage(), e);
		}
		
	}

	public void loginOffline(UserVO userVO) throws BackofficeHandlerException {
		
		try {
			
			UserAndroid userAndroid = parseToUserAndroid(userVO);

			loginManager.loginOffline(userAndroid);
			
			parseToUserVO(userVO, userAndroid);

			
		} catch (BackofficeManagerException e) {
			
			throw new BackofficeHandlerException(e.getMessage(), e);
			
		}
		
	}

	public UserVO checkLastLogin() throws BackofficeHandlerException {
		try {
			
			UserAndroid userAndroid = loginManager.checkLastLogin();
			
			if (userAndroid == null) {
				return null;
			}
			
			UserVO userVO = parseToUserVO(userAndroid);
			
			return userVO;
			
		} catch (BackofficeLoginOffLineExpireException e) {		
			return null;
		} catch (BackofficeManagerException e) {
			throw new BackofficeHandlerException(e.getMessage(), e);
			
		}		
	}
	

	public UserVO lastLogin() throws BackofficeHandlerException {
		try {
			
			UserAndroid userAndroid = loginManager.lastLogin();
			
			if (userAndroid == null) {
				return null;
			}
			
			UserVO userVO = parseToUserVO(userAndroid);
			
			return userVO;
			
		} catch (BackofficeManagerException e) {

			throw new BackofficeHandlerException(e.getMessage(), e);
			
		}		
	}	
	
	private UserVO parseToUserVO(UserVO userVO, UserAndroid userAndroid) {
		
		userVO.setImei(userAndroid.getImei());
		userVO.setLastLogin(userAndroid.getLastLogin());
		userVO.setImei(userAndroid.getImei());
		userVO.setName(userAndroid.getName());
		userVO.setBrand(userAndroid.getBrand());
		userVO.setModel(userAndroid.getModel());
		return userVO;
		
	}		

	private UserVO parseToUserVO(UserAndroid userAndroid) {
		
		UserVO userVO = new UserVO();
		
		parseToUserVO(userVO, userAndroid);
		
		return userVO;
		
	}	
	
	private UserAndroid parseToUserAndroid(UserVO userVO) {
		
		UserAndroid userAndroid = new UserAndroid();
		userAndroid.setImei(userVO.getImei());
		userAndroid.setName(userVO.getName());
		userAndroid.setLastLogin(userVO.getLastLogin());
		userAndroid.setBrand(userVO.getBrand());
		userAndroid.setModel(userVO.getModel());
		
		return userAndroid;
		
	}	
		

}
